package org.alex.samples.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IExamScore;
import org.alex.samples.dao.IExamUserSubDAO;
import org.alex.samples.dao.ISubDAO;
import org.alex.samples.dao.IUserDAO;
import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamSubScoreVO;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamTestTypeVO;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.ExamUserSub;
import org.alex.samples.model.ExamUserVO;
import org.alex.samples.model.ShowExamScore;
import org.alex.samples.model.Sub;
import org.alex.samples.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SubServiceImpl implements ISubService {
	@Autowired
	private ISubDAO subDAO;
	
	@Autowired 
	private IExamScore examScore;
	
	@Autowired 
	private IExamUserSubDAO examUserSubDAO;
	@Autowired 
	private IUserDAO userDAO;
	//通过科目id查询查询到当前科目所有参考学生的成绩
	@Override
	public List<ExamScoreVO> getAllScoreBysubId(int subId){
		List<ExamScoreVO> resultList = new ArrayList<ExamScoreVO>();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		ExamScoreVO vo ;
		List<ExamScore> esLists = examScore.getAllScoreBysubId(subId);
		
		for(ExamScore esList : esLists){
			vo = new ExamScoreVO();
			BeanUtils.copyProperties(esList, vo);
			vo.setSubjectName(subDAO.getSubById(esList.getSubId()).getName());
			vo.setStudentName(userDAO.getUserById(esList.getUserId()).getName());
			vo.setExamTimeString(df.format(esList.getExamTime()));
			resultList.add(vo);
		}
		return resultList;
		
	}
	
	@Override
	public List<ExamSubjectVO> findAllSubs() {
		List<Sub> subjectList = subDAO.findAllSubs();
		List<ExamSubjectVO> resultList = new ArrayList<ExamSubjectVO>();
		for(Sub subject : subjectList){
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			resultList.add(vo);
		}
		
		return resultList;
	}

	@Override
	public Sub getSubById(int id) {
		
		return subDAO.getSubById(id);
	}

	@Override
	public void createSub(Sub sub) throws Exception {
		
		subDAO.createSub(sub);
	}

	@Override
	public void editSub(Sub sub) throws Exception {
		
		 subDAO.editSub(sub);
	}

	@Override
	public void deleteSub(int id) throws Exception {
		
		 subDAO.deleteSub(id);
	}

	@Override
	public List<String> getAllUserBySubject(Integer subjectId) {
		
		// TODO Auto-generated method stub
		return subDAO.getAllUserBySubject(subjectId);
	}

	@Override
	public void saveSubjectStudent(List<String> subjectStudents, Integer subjectId) throws RuntimeException {
       List<String> dbSubjectUserList = subDAO.getAllUserBySubject(subjectId);//获取当前科目关联的所有学生
       List<String> insertList = new ArrayList<String>();
       List<String> delList = new ArrayList<String>();
       //1有，2无的情况
       //判断哪些学生是新勾选上的
       for(String selectedUser : subjectStudents){
    	   if(!dbSubjectUserList.contains(selectedUser)){
    		   insertList.add(selectedUser);
    	   }
       }
       //1无，2有的情况
       //判断哪些学生本来是勾选上的，现在去掉勾选的
       for(String selectedUser : dbSubjectUserList){
    	   if(!subjectStudents.contains(selectedUser)){
    		   delList.add(selectedUser);
    	   }
       }
       
       subDAO.insertSubjectUsers(insertList, subjectId);
       subDAO.deleteSubjectUsers(delList, subjectId);
       
		
	}

	@Override
	public List<ExamSubScoreVO> getAllUserSubs(User userVO) {
		List<ExamSubScoreVO> resultList = new ArrayList<ExamSubScoreVO>();
		List<ExamUserSub> list = examUserSubDAO.getAllSubjectByUser(userVO.getId());
		for(ExamUserSub sub : list){
			ExamSubScoreVO subVO = new ExamSubScoreVO();
			subVO.setSubId(sub.getSub().getId());
			subVO.setSubName(sub.getSub().getName());
			subVO.setDescription(sub.getSub().getDescription());
			subVO.setStatus(sub.getSub().getStatus());
			subVO.setTest_count(sub.getSub().getTest_count());
			subVO.setTotal_score(sub.getSub().getTotal_score());
			subVO.setTotal_time(sub.getSub().getTotal_time());
			ExamScoreVO score = examScore.getScore(userVO.getId(), subVO.getSubId());
			if(score !=null){
				subVO.setExamScoreVO(score);
			}
			resultList.add(subVO);
		}
		
		return resultList;
	}

	@Override
	public ExamScoreVO getScoreById(Integer scoreId) {
		ExamScoreVO vo = examScore.getScoreById(scoreId);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		vo.setExamTimeString(df.format(vo.getExamTime()));
		return vo;
	}

	@Override
	public List<ExamScoreVO> findAllScore() {
		
		List<ExamScore> list = examScore.findAllScore();
		List<ExamScoreVO> resultList = new ArrayList<ExamScoreVO>();
		for(ExamScore score : list){
			ExamScoreVO vo = new ExamScoreVO();
			vo.setId(score.getId());
			vo.setUserId(score.getUserId());
			Sub sub = subDAO.getSubById(score.getSubId());
			
			
				vo.setSubjectName(sub.getName());
			
			ExamUserVO userVO = new ExamUserVO();
			vo.setUserS(userVO);
			vo.setExamTime(score.getExamTime());
			resultList.add(vo);
		}
		return resultList;
		

	}

}
